package com.gzyz.cheetah.controller;


import cn.hutool.core.bean.BeanUtil;
import com.gzyz.cheetah.annotations.AdminJwtIntercept;
import com.gzyz.cheetah.entity.dto.AdminDTO;
import com.gzyz.cheetah.entity.table.Admin;
import com.gzyz.cheetah.service.IAdminService;
import com.gzyz.cheetah.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author xclhove
 * @since 2023-11-06
 */
@RestController
@RequestMapping("/admin")
@Api("管理员")
public class AdminController {
    @Autowired
    IAdminService iadminService;
    
    @GetMapping("/login")
    @ApiOperation("管理员登录")
    public Result <String> login(@RequestParam String username,@RequestParam String password) {
        String token = iadminService.login(username, password);
        return Result.success(token);
    }
    @PostMapping("/register")
    @ApiOperation("管理员注册")
    public Result<Object> register(@RequestBody AdminDTO adminDTO) {
        Admin admin = BeanUtil.copyProperties(adminDTO, Admin.class);
        iadminService.register(admin);
        return Result.success(admin);
    }


    @AdminJwtIntercept
    @PostMapping("/delete/{id}")
    @ApiOperation("通过id删除管理员")
    public Result <Object> delete(@PathVariable Integer id) {
        iadminService.deleteAdminById(id);
        return Result.success();
    }


    @AdminJwtIntercept
    @PostMapping("/update")
    @ApiOperation("更新管理员信息")
    public Result <Object> update(@RequestBody Admin admin) {
        iadminService.update();
        return Result.success();
    }

}
